package com.pox.guli.service.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pox.guli.service.base.dto.MemberDto;
import com.pox.guli.common.base.result.ResultCodeEnum;
import com.pox.guli.common.base.util.FormUtils;
import com.pox.guli.common.base.util.JwtInfo;
import com.pox.guli.common.base.util.JwtUtils;
import com.pox.guli.common.base.util.MD5;
import com.pox.guli.service.base.exception.PoXException;
import com.pox.guli.service.ucenter.entity.Member;
import com.pox.guli.service.ucenter.entity.vo.LoginVo;
import com.pox.guli.service.ucenter.entity.vo.RegisterVo;
import com.pox.guli.service.ucenter.mapper.MemberMapper;
import com.pox.guli.service.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author pox21s
 * @since 2022-06-05
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Override
    public void register(RegisterVo registerVo) {

        String code = registerVo.getCode();
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();

        //校验参数
        if (StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(nickname)) {
            throw new PoXException(ResultCodeEnum.PARAM_ERROR);
        }

        // 校验 验证码
        // TODD

        //是否被注册
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);

        Integer count = baseMapper.selectCount(queryWrapper);

        if(count > 0){
            throw new PoXException(ResultCodeEnum.REGISTER_MOBLE_ERROR);
        }

        // 通过验证 注册用户
        Member member = new Member();
        member.setPassword(MD5.encrypt(mobile));
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setDisabled(false);
        // 初始用户设置为默认头像
        member.setAvatar("\n" +
                "https://guli-file-pox21s.oss-cn-beijing.aliyuncs.com/avatar/%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8F.jpg");

        baseMapper.insert(member);
    }

    @Override
    public String login(LoginVo loginVo) {

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        // 校验参数
        if (StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)) {
            throw new PoXException(ResultCodeEnum.PARAM_ERROR);
        }

        // 校验手机号
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Member member = baseMapper.selectOne(queryWrapper);

        if(member == null){
            throw new PoXException(ResultCodeEnum.LOGIN_ERROR);
        }

        // 校验密码
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new PoXException(ResultCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        // 检验用户是否被禁用
        if(member.getDisabled()){
            throw new PoXException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }

        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(member.getId());
        jwtInfo.setNickname(member.getNickname());
        jwtInfo.setAvatar(member.getAvatar());

        // 第二个参数设定了作用时间
        return JwtUtils.getJwtToken(jwtInfo, 1800);
    }

    @Override
    public Member getByOpenid(String openid) {

        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public MemberDto getMemberDtoByMemberId(String memberId) {

        Member member = baseMapper.selectById(memberId);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member, memberDto);

        return memberDto;
    }

    @Override
    public Integer countRegisterNum(String day) {
        return baseMapper.selectRegisterNumByDay(day);
    }
}
